# -*- coding: utf-8 -*-
#!/usr/bin/env python

from flask_sqlalchemy import SQLAlchemy

print("sss")
db = SQLAlchemy()

'''
SQLAlchemy用到的主要类型
db.String
db.Text
db.Integer
db.Float
db.Boolean
db.Date
db.DateTime
db.Time
'''
#name text, link text primary key, detail text, img text



class Spiderdb(db.Model):
    __tablename__ = "spider_user_control"
    id = db.Column(db.Integer(), primary_key=True)
    user_insert_time = db.Column(db.String())
    user_last_use_time = db.Column(db.String())
    user_appid = db.Column(db.String(),unique=True)
    user_time_control = db.Column(db.String())
    user_appkey = db.Column(db.String())
    user_token = db.Column(db.String())
    


    def __init__(self, user_insert_time, user_last_use_time,user_appid,user_time_control,user_appkey,user_token):                #如果没有定义，会自动创建。所有定义的字段会成为此方法接受的关键字 
        self.user_insert_time = user_insert_time                       # User 给入的参数
        self.user_last_use_time = user_last_use_time
        self.user_appid = user_appid
        self.user_time_control = user_time_control
        self.user_appkey = user_appkey
        self.user_token = user_token
        

    def to_json(self):
        js = {
            'user_insert_time':self.user_insert_time,
            'user_last_use_time':self.user_last_use_time,
            'user_appid':self.user_appid,
            'user_time_control':self.user_time_control,
            'user_appkey':self.user_appkey,
            'user_token':self.user_token
        }
        return js

class SpiderWork(db.Model):
    __tablename__ = "spider_worker_list"
    id = db.Column(db.Integer(), primary_key=True)
    user_appkey = db.Column(db.String(),unique=True)
    user_set_catgory = db.Column(db.Integer())
    user_auto_catgory = db.Column(db.String())
    user_item_work1 = db.Column(db.String())
    user_item_work2 = db.Column(db.String())
    user_item_work3 = db.Column(db.String())
    user_item_num = db.Column(db.Integer())
    user_today_work_num = db.Column(db.Integer())

    def __init__(self, user_appkey,user_set_catgory,user_auto_catgory, user_item_work1,user_item_work2,user_item_work3,user_item_num,user_today_work_num):                #如果没有定义，会自动创建。所有定义的字段会成为此方法接受的关键字 
        self.user_appkey = user_appkey  
        self.user_set_catgory = user_set_catgory
        self.user_auto_catgory = user_auto_catgory                  
        self.user_item_work1 = user_item_work1
        self.user_item_work2 = user_item_work2
        self.user_item_work3 = user_item_work3
        self.user_item_num = user_item_num
        self.user_today_work_num = user_today_work_num

    def to_json(self):
        js = {
            'user_appkey':self.user_appkey,
            'user_set_catgory':self.user_set_catgory,
            'user_auto_catgory':self.user_auto_catgory,
            'user_item_work1':self.user_item_work1,
            'user_item_work2':self.user_item_work2,
            'user_item_work3':self.user_item_work3,
            'user_item_num':self.user_item_num,
            'user_today_work_num':self.user_today_work_num
        }
        return js

def runtest():
    res = Spiderdb(user_insert_time='2020-07-15 12:22',
                                user_last_use_time = "2020-07-15 12:22",
                                user_appid = '62 女士',
                                user_time_control = 'default',
                                user_appkey = 'xxxxxxxxxxxx', 
                                user_token = '40',
                                          )
    res.create()
    db.session.add(res)
    db.session.commit()

if __name__ == '__main__':
    res = Spiderdb(user_insert_time='2020-07-15 12:22',
                                user_last_use_time = "2020-07-15 12:22",
                                user_appid = '62 女士',
                                user_time_control = 'default',
                                user_appkey = 'xxxxxxxxxxxx', 
                                user_token = 40,
                                          )
   
    db.session.add(res)
    db.session.commit()
#

'''
#创建的USer模型  #SQLAlchemy 允许创建的数据模型，表结构这里也有
class User(db.Model):
    __tablename__ = 'yuc_sqlite'      # 表的名字
    # 表的结构:
    id = db.Column(db.Integer, primary_key=True)        #primary_key 会告诉SQLAlchemy 这个字段需要做"主键索引"。每个SQLAlchemy都要有一个主键才能正常工作 
    #username = db.Column("user_time_control",db.String(255))   #也是可以这么写的，第一个参数为可选参数。如果不写就会认为这个字段名与这个属性的名字是一样的
    username = db.Column(db.String(255), unique=True)
    email = db.Column(db.String(255), unique=True)
    posts=db.relationship('Post',                       #关联对象，让Post可以访问到
        backref='yuc_sqlite',                           #backref参数可以使我们通过Pist.user属性对User对象进程读取和修改
        lazy='dynamic')                                 #动态方式，这个有点长。大概就是使用的时候再加载

    def __init__(self, username, email):                #如果没有定义，会自动创建。所有定义的字段会成为此方法接受的关键字 
        self.username = user_time_control                       # User 给入的参数
        self.email = email

    def __repr__(self):
        return "<User '{}'>".format(self.username)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255))
    text = db.Column(db.Text())
    publish_date = db.Column(db.DateTime())
    yuc_sqlite_id = db.Column(db.Integer(),db.ForeignKey('yuc_sqlite.id'))   #外键约束
    #强制要求yuc_sqlite_id 字段的值存在于yuc_sqlite表中的id列中，这是数据库进行的一项检查，用来保证每个post对象都会对应到一个已有的yuc_sqlite
    def __init__(self,title):
        self.title = title

    def __repr__(self):
        return "<Post '{}'>".format(self.title)
'''
